﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Moonlite.Data.Mpq;
using Moonlite.ExtensionApi.ControlInterfaces;
using DevExpress.XtraBars;

namespace Moonlite.Modules.Editors.DataTools
{
    public partial class DataViewer : Form, IDocumentEditor
    {
        public void Open(string path)
        {
            string displayPath = path.Replace(DataCustomFolderNode.DataFilesMpqFolderPath, "");
            using (TMPQArchive archive = new TMPQArchive(DataCustomFolderNode.instance.Project.GetMapPath()))
            {
                XmlDocument document = new XmlDocument();
                string tempFile = Path.GetTempFileName();
                archive.ExportFile(path, tempFile);
                document.Load(tempFile);
                xmlTreeView.AddDocumentToRootNode(document, displayPath);
            }
        }

        private void Search()
        {
            if (!string.IsNullOrEmpty((string)searchBox.EditValue))
            {
                xmlTreeView.Search((string)searchBox.EditValue);
            }
        }

        private void OnSearchBoxEditValueChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void OnSearchItemClick(object sender, ItemClickEventArgs e)
        {
            Search();
        }

        public DataViewer()
        {
            InitializeComponent();
        }
    }
}
